﻿using System;
using System.Web.Mvc;
using ProjetoComperio.BusinessLogicLayer;
using ProjetoComperio.DataLayer.Entities;
using ProjetoComperio.Framework.Web.Security.UrlEncrypt;
using ProjetoComperio.SystemFramework.Web;

namespace ProjetoComperio.Presentation.Web.Controllers
{
    public class AcademicPersonCVController : ComperioController
    {
        private AcademicPersonCVBLL AcademicPersonCVBLL;

        public AcademicPersonCVController()
        {
            AcademicPersonCVBLL = new AcademicPersonCVBLL();
        }
        
        public AcademicPersonCVController (AcademicPersonCVBLL academicPersonCVBLL)
        {
            this.AcademicPersonCVBLL = academicPersonCVBLL;
        }
        
        #region Edit
        [AcceptVerbs(HttpVerbs.Get)]
        public ActionResult Edit(string id)
        {                        
            AcademicPersonCV academicPersonCV = new AcademicPersonCV();            
            academicPersonCV = null;
            academicPersonCV = AcademicPersonCVBLL.CatchAcademicPersonCV(Convert.ToInt32(this.CurrentUser.PersonID));
            if (academicPersonCV != null)
            {
                Session["AcademicPersonCVID"] = academicPersonCV.AcademicPersonCVID;
                return View(academicPersonCV);
            }
            else
                return RedirectToAction("Create");
        }

        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult Edit([Bind(Exclude = "id")] AcademicPersonCV academicPersonCV)
        {        
            if(!ModelState.IsValid)                
                return View(academicPersonCV);

            string id = Convert.ToString(Session["AcademicPersonCVID"]);            
            var CV = AcademicPersonCVBLL.CatchAcademicPersonCV(Convert.ToInt32(id));

            CV.CVDescription = academicPersonCV.CVDescription;

            AcademicPersonCVBLL.Edit(CV);

            return Redirect("/Home/StartPage");
        }

        #endregion

        #region Create

        [HttpGet]
        public ActionResult Create()
        {
            return View();
        }

        [HttpPost]
        public ActionResult Create(AcademicPersonCV AcademicPersonCV)
        {
            if (!ModelState.IsValid)
                return View(AcademicPersonCV);

            AcademicPersonCV.AcademicPersonCVID = Convert.ToInt32(Convert.ToInt32(this.CurrentUser.PersonID));
            AcademicPersonCVBLL.Create(AcademicPersonCV);

            return Redirect("/Home/StartPage");
        }

        #endregion 



    }
}
